package com.tuanzi.loan.web.bean.family;

import static com.tuanzi.loan.business.constants.DictionaryCode.API_SERIAL;
import static com.tuanzi.loan.business.constants.DictionaryCode.API_STATUS;
import static com.tuanzi.loan.business.constants.DictionaryCode.APPROVAL_TYPE;
import static com.tuanzi.loan.business.constants.DictionaryCode.CREDIT_CARD_TAG;
import static com.tuanzi.loan.business.constants.DictionaryCode.FIRST_PARTY_LOAN_TYPE;
import static com.tuanzi.loan.business.constants.DictionaryCode.FIRST_UNION_RESULT;
import static com.tuanzi.loan.business.constants.DictionaryCode.IDENTITY_TAG;
import static com.tuanzi.loan.business.constants.DictionaryCode.LOAN_NECESSARY_DATA;
import static com.tuanzi.loan.business.constants.DictionaryCode.PROMOTION_TYPE;
import static com.tuanzi.loan.business.constants.DictionaryCode.RATE_TYPE;
import static com.tuanzi.loan.business.constants.DictionaryCode.UNION_INFO;
import static com.tuanzi.loan.business.constants.DictionaryCode.UNION_RESULT;
import static com.tuanzi.loan.business.constants.DictionaryCode.UNION_SERIAL;
import static com.tuanzi.loan.business.constants.DictionaryCode.USER_LABEL_AGE;
import static com.tuanzi.loan.business.constants.DictionaryCode.USER_LABEL_CHANNELSOURCE;
import static com.tuanzi.loan.business.constants.DictionaryCode.USER_LABEL_INCOME;
import static com.tuanzi.loan.business.constants.DictionaryCode.USER_LABEL_LOANAPP;
import static com.tuanzi.loan.business.constants.DictionaryCode.USER_LABEL_MONEY;
import static com.tuanzi.loan.business.constants.DictionaryCode.USER_LABEL_PROFESSION;
import static com.tuanzi.loan.business.constants.DictionaryCode.USER_LABEL_ZHAMACREDIT;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import lombok.Data;
import lombok.EqualsAndHashCode;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.primefaces.component.tabview.TabView;
import org.primefaces.event.TabChangeEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.service.family.UserInfoService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.view.family.UserTagView;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class UserTagInfoBean extends JSFInfoBean<UserTagView> {

	private static final long serialVersionUID = -2152778554108785342L;

	@Autowired
	private UserInfoService userInfoService;
	private Integer activeTabIndex;

	@PostConstruct
	private void init() {
		String id = getParamFromRequest("id");
		if (StringUtils.isNotBlank(id)) {
			$ = userInfoService.loanUserTagView(NumberUtils.toLong(id));
			filterSensitiveInfo($);
		} else {
			$ = new UserTagView();
		}
		activeTabIndex = 0;
	}

	public void onSaveUserLabel() {
		userInfoService.saveUserTagView($);
		addSuccessfullyMessage();
	}

	public void onChangeTabView(TabChangeEvent event) {
		TabView tv = (TabView) event.getComponent();
		this.activeTabIndex = tv.getActiveIndex();
	}
	
	/**
	 * 过滤敏感信息
	 */
	private void filterSensitiveInfo(UserTagView userInfo) {
		userInfo.getUserInfoView().setMobnum(PhoneNumNideUtil.hideNum(userInfo.getUserInfoView().getMobnum()));
		userInfo.getUserInfoView().setIdcard(PhoneNumNideUtil.hideIdCard(userInfo.getUserInfoView().getIdcard()));
		userInfo.getUserInfoView().setBankCardNumber(PhoneNumNideUtil.hideBankCard(userInfo.getUserInfoView().getBankCardNumber()));
		userInfo.getUserInfoView().setPetname(PhoneNumNideUtil.hideName(userInfo.getUserInfoView().getPetname()));
		userInfo.getUserInfoView().setCensusAddress(PhoneNumNideUtil.hideAddress(userInfo.getUserInfoView().getCensusAddress()));
		userInfo.getUserInfoView().setFullAddress(PhoneNumNideUtil.hideAddress(userInfo.getUserInfoView().getFullAddress()));
	}

	@Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(FIRST_PARTY_LOAN_TYPE, APPROVAL_TYPE, CREDIT_CARD_TAG, IDENTITY_TAG, LOAN_NECESSARY_DATA, PROMOTION_TYPE, RATE_TYPE, UNION_INFO, UNION_SERIAL,
				API_SERIAL, FIRST_UNION_RESULT, UNION_RESULT, API_STATUS, USER_LABEL_AGE, USER_LABEL_CHANNELSOURCE, USER_LABEL_INCOME, USER_LABEL_LOANAPP, USER_LABEL_MONEY,
				USER_LABEL_PROFESSION, USER_LABEL_ZHAMACREDIT);
	}

}
